package com.springboot.demo.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.cx.item.common.exception.CommonException;
import com.cx.item.common.par.PageParam;
import com.cx.item.common.service.impl.BaseServiceImpl;
import com.cx.item.common.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.mapper.GoodsMapper;
import com.springboot.demo.mapper.GoodsTypeMapper;
import com.springboot.demo.mapper.OrderMapper;
import com.springboot.demo.model.enums.GoodsTypeEnum;
import com.springboot.demo.model.example.GoodsTypeExample;
import com.springboot.demo.model.example.OrderExample;
import com.springboot.demo.model.model.Customer;
import com.springboot.demo.model.model.Goods;
import com.springboot.demo.model.model.GoodsType;
import com.springboot.demo.model.model.Order;
import com.springboot.demo.model.par.OrderPar;
import com.springboot.demo.model.vo.OrderVo;
import com.springboot.demo.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by heavin on 2018/6/21.
 */
@Service
public class OrderServiceImpl extends BaseServiceImpl<Order, OrderExample, OrderMapper> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;


    @Override
    public Order getOrderByOrderNumber(String orderNumber) {

        if (StrUtil.isEmpty(orderNumber)) {
            return null;
        }

        OrderExample orderExam = new OrderExample();
        OrderExample.Criteria orderExamCriteria = orderExam.createCriteria();
        orderExamCriteria.andOrderNumberEqualTo(orderNumber);
        List<Order> orderList = orderMapper.selectByExample(orderExam);
        if (CollUtil.isNotEmpty(orderList)) {
            return orderList.get(0);
        }

        return null;
    }

    @Override
    public Customer getCustomerByOrderId(int orderId) {
        return orderMapper.getCustomerByOrderId(orderId);
    }

    @Override
    public int create(OrderPar orderPar) {
        Order order = new Order();
        BeanUtil.copyProperties(orderPar, order);

        //插入商品名称  类型 商品id  商品价格就是订单价格
        Integer goodsId = orderPar.getGoodsId();
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        if (null == goods) {
            throw new CommonException("此商品不存在");
        }
        GoodsTypeExample goodsTypeExample = new GoodsTypeExample();
        GoodsTypeExample.Criteria goodsTypecriteria = goodsTypeExample.createCriteria();
        goodsTypecriteria.andGoodsTypeCodeEqualTo(goods.getGoodsTypeCode());
        List<GoodsType> goodsTypesList = goodsTypeMapper.selectByExample(goodsTypeExample);
        if (CollUtil.isEmpty(goodsTypesList)) {
            throw new CommonException("此商品类型不存在");
        }
        if (GoodsTypeEnum.CODE0100.getValue().equals(goods.getGoodsTypeCode()) && null == orderPar.getStaffId()) {
            throw new CommonException("请传入有效服务员");
        }

        GoodsType goodsType = goodsTypesList.get(0);
        order.setGoodsName(goods.getGoodsName());
        order.setGoodsType(goodsType.getGoodsType());
        order.setGoodsId(goods.getGoodsId());
        order.setGoodsTypeCode(goods.getGoodsTypeCode());


        //月嫂类的服务内容 新生儿 / 新生儿 预产期 2018-01-01 五星月嫂     非新生儿 / 男 0-3个月 金牌月嫂
        if (GoodsTypeEnum.CODE0100.getValue().equals(goods.getGoodsTypeCode())) {
            order.setServeContent(this.getServeContent(order));
            //月嫂的价格  由前端传入
            order.setOrderPrice(orderPar.getOrderPrice());
        } else {
            order.setServeContent(goods.getServeContent());
            BigDecimal goodsPrice = goods.getGoodsPrice();
            if (null != goodsPrice) {
                order.setOrderPrice(goodsPrice);
            }
        }

        order.setCreateTime(new Date());
        order.setOrderStatus(1);
        //定期保洁下单成功后 状态为代付款   其他的为待确认
        if (GoodsTypeEnum.CODE0900.getValue().equals(goods.getGoodsTypeCode())) {
            order.setPayStatus(2);
        } else {
            order.setPayStatus(1);
        }

        // 生成订单编号
        StringBuffer orderNoSb = new StringBuffer();
        String dateFormat = DateUtil.getDateFormat(new Date(), "yyyyMMddHHmmss");
        orderNoSb.append("ON");
        orderNoSb.append(dateFormat);
        orderNoSb.append(RandomUtil.randomInt(9));
        orderNoSb.append(RandomUtil.randomInt(9));
        order.setOrderNumber(orderNoSb.toString());

        int result = orderMapper.insertSelective(order);
        return result;

    }

    /**
     * 月嫂服务内容：
     * 新生儿 / 新生儿 预产期 2018-01-01 五星月嫂
     * 非新生儿 / 男 0-3个月 金牌月嫂
     *
     * @param order
     * @return
     */
    private String getServeContent(Order order) {
        StringBuilder ServeContent = new StringBuilder();
        if (order.getIsBaby() == 1) {
            ServeContent.append("新生儿 预产期 ");
            ServeContent.append(DateUtil.yyyyMMddFormat(order.getBabyExpectedDate()) + " ");

        } else {
            ServeContent.append("非新生儿 ");
            if (order.getBabySex() == 1) {
                ServeContent.append("男 ");
            } else if (order.getBabySex() == 0) {
                ServeContent.append("女 ");
            }
            ServeContent.append(order.getBabyMonthsAge() + " ");
        }
        ServeContent.append(order.getServiceLevel());
        return ServeContent.toString();
    }

    @Override
    public PageInfo<List<OrderVo>> queryList(Integer payStatus, Integer customerId, PageParam pageParam) {
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andOrderStatusEqualTo(1);
        criteria.andCustomerIdEqualTo(customerId);
        if (null == payStatus || 0 == payStatus) {
            //查询全部
        } else {
            criteria.andPayStatusEqualTo(payStatus);
        }

        example.setOrderByClause("create_time desc");

        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Order> orderList = orderMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(orderList);

        List<OrderVo> orderVoList = new ArrayList<>();
        OrderVo orderVo;
        for (Order order : orderList) {
            orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);

            String goodsTypeCode = orderVo.getGoodsTypeCode();
            // 定期保洁的支付 是 按照订单价格支付  其他的由用户随意支付
            if (!StringUtils.isEmpty(goodsTypeCode) && "0900".equals(goodsTypeCode)) {
                orderVo.setOderPriceType(1);
            } else {
                orderVo.setOderPriceType(2);
            }

            orderVoList.add(orderVo);
        }

        pageInfo.setList(orderVoList);
        return pageInfo;
    }


    @Override
    public OrderVo queryById(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        if (null == order) {
            throw new CommonException("您查询的订单不存在");
        }
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(order, orderVo);
        String goodsTypeCode = orderVo.getGoodsTypeCode();
        // 定期保洁的支付 是 按照订单价格支付  其他的由用户随意支付
        if (!StringUtils.isEmpty(goodsTypeCode) && "0900".equals(goodsTypeCode)) {
            orderVo.setOderPriceType(1);
        } else {
            orderVo.setOderPriceType(2);
        }
        return orderVo;
    }
}
